// pages/join-group/index.js
var url = getApp().globalData.ApiUrl;

var timeformat = require('../../utils/timeformat.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   * alertTips   弹窗按钮文字
   * alertTitle  弹窗价格名称
   * buyer_id    如果是参团，才会有这个id
   * status      选中当前状态，1普通购买，2拼团包括新建团和参团购买，3砍价购买
   */
  data: {
    hr: '', //小时
    min: '', //分钟
    sec: '', //秒
    minusStatus: "disabled",    //使用data数据设置样式名
    showLuckMoney: true,
    // index: 0,     //礼盒下标
    imgUrl: [],
    current: 1,  // 图片所在滑块位置
    sum: 0,     //商品图片总数
    goods_details: {}, //商品详情
    storeItemList: [], //店内商品推荐
    goods_price: '', // 商品价格
    goods_storage: '', //商品库存
    showView: false,
    showPin: false,
    showKan: false,
    num: 1,
    alertTips: '马上开团',
    alertTitle: "优惠价",
    log_id: '',
    status: 2,
    pinlist: '', //参加该商品的拼团列表
    length: '', //开团数量
    single: false, //是否单独购买
    goods_img: '',
    propertyChildNames: '',
    showSure: true,
    insertTime: [],  // 倒计时
  },
  count_down: function (allsec) {
    var that = this;
    clearTimeout(that.data.timer);
    that.setData({
      day: timeformat.timeformat(allsec).days + '天',
      hr: timeformat.timeformat(allsec).hrs,
      min: timeformat.timeformat(allsec).mins,
      sec: timeformat.timeformat(allsec).second
    });
    if (allsec <= 0) {
      that.setData({
        day: '0天',
        hr: '00',
        min: '00',
        sec: '00'
      });
      // timeout则跳出递归
      return;
    };
    that.data.timer = setTimeout(function () {
      // 放在最后--
      allsec -= 1000;
      that.count_down(allsec);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      goods_id: options.goods_id
    })
    //获取商品详情
    wx.request({
      url: url + '/mobile/index.php?act=goods&op=goods_detail',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        goods_id: options.goods_id,
        key: wx.getStorageSync("key")
      },
      success: (res) => {
        if (res.data.code == 200) {
          var datas = res.data.datas;
          var specObj = res.data.datas.goods_info.spec_name;
          var imgURL = res.data.datas.goods_image.split(',');
          var tmp = {}; //商品规格
          var tmp2 = {}; //商品尺寸
          var specALLArr = []; // 商品贵和和商品
          var goods_spec = [];
          var spec_id = [];
          for (var spec_d in res.data.datas.goods_info.goods_spec) {
            goods_spec.push(res.data.datas.goods_info.goods_spec[spec_d]);  // 商品规格
            spec_id.push(spec_d);
            var spec = goods_spec.toString().replace(/,/g, " ");
          };
          for (var i in specObj) {
            tmp.id = i;
            tmp.name = specObj[i];
            tmp.twoData = [];
            for (var j in res.data.datas.goods_info.spec_value[i]) {
              tmp2.id = j;
              tmp2.name = res.data.datas.goods_info.spec_value[i][j];
              tmp2.propertyID = i;
              tmp2.active = (j in res.data.datas.goods_info.goods_spec) ? true : false;
              tmp.twoData.push(JSON.parse(JSON.stringify(tmp2)));
            }
            specALLArr.push(JSON.parse(JSON.stringify(tmp)));
          };
          var pintuan_kan_price = '';
          if (datas.goods_info.kanjia_end_time) {
            var endTime = datas.goods_info.kanjia_end_time * 1000;  //砍价倒计时
            that.count_down(endTime); //砍价倒计时
            pintuan_kan_price = datas.goods_info.kanjia_price;
          }
          if (datas.goods_info.pintuan_end_time) {
            var endTime = datas.goods_info.pintuan_end_time * 1000;  //拼团倒计时
            that.count_down(endTime); //拼团倒计时  
            pintuan_kan_price = datas.goods_info.pintuan_price;
          }
          if (datas.goods_info.promotion_type == 'xianshiall') {
            that.setData({
              goods_price: datas.goods_info.promotion_price, //商品价格
            })
          } else if (datas.goods_info.promotion_type == 1) {
            that.setData({
              goods_price: datas.goods_info.pintuan_price, //商品价格
            })
          } else if (datas.goods_info.kanjia_promotion == 1) {
            that.setData({
              goods_price: datas.goods_info.kanjia_price, //商品价格
            })
          } else if (datas.goods_info.promotion_type == "newbie") {
            this.setData({
              goods_price: datas.goods_info.promotion_price
            })
          } else {
            that.setData({
              goods_price: datas.goods_info.goods_price, //商品价格
            });
          };
          if (spec) {
            this.setData({
              propertyChildNames: spec,
            })
          };
          var prent = datas.goods_info.goods_salenum / datas.goods_info.goods_storage;
          that.setData({
            imgUrl: imgURL,
            sum: imgURL.length,
            goods_details: res.data.datas,
            specALLArr: specALLArr,
            detailData: datas,  //商品详情
            buyGoods_id: options.goods_id, //购买的商品价格
            pintuan_kan_price: pintuan_kan_price, //拼团或砍价价格
            goods_storage: datas.goods_info.goods_storage, //商品库存
            prent: prent,        // 百分比
          });
          
          that.getStoreRecommend(res.data.datas.store_info.store_id);
          WxParse.wxParse('article', 'html', datas.goods_body, that, 5);
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      }
    });
    that.getPintuan();
  },
  //获取该商品的其他拼团
  getPintuan: function(e) {
    wx.showLoading({
      title: '加载中，请稍后...',
    });
    var that = this;
    wx.request({
      url: url + '/mobile/index.php?act=api&op=get_wait_pintuan',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        goods_id: that.data.goods_id,
        key: wx.getStorageSync('key'),
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.hideLoading();
          var pinlist = res.data.datas.pintuan;
          var length = pinlist.length;
          for (var i in pinlist) {
            var now = new Date(); //开始时间
            // var endTime = pinlist[i].end_time * 1000;
            // var differ = endTime - now.getTime();
            // timeformat.timeformat(differ); //计算时间差
            // var endToTime = timeformat.timeformat(differ);
            // if (endToTime.days) {
            //   var ending = endToTime.days + '天' + endToTime.hrs + ':' + endToTime.mins + ":" + endToTime.second;
            // } else {
            //   var ending = endToTime.hrs + ':' + endToTime.mins + ":" + endToTime.second;
            // }
            // that.setData({
            //   ending: ending,
            // });
            var tim = [];
            tim.push(parseInt(pinlist[i].end_time) - now)
            that.count_down(tim);
          };
          that.setData({
            pinlist: pinlist,
            length: length,
          });
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  /**
   * 发起砍价
   */
  toCreateBargin: function (e){
    var that = this;
    var img = that.data.imgUrl[0];
    that.setData({
      showKan: true,
      goods_img: img,
    });
    var dataset = e.currentTarget.dataset; 
  },
  /**
   * 立即砍价
   */
  barginNow: function (e){
    var that = this;
    /**
     * log_id 参团id,自己发起的团,支付成功后返回的id，有则传，无则不传，不能穿空    pintuan_id 不为空时，为拼主的拼团号
     * pintuan    拼团的话设置为1，默认为空
     * status 1普通购买，2拼团，3秒杀
     */
    if (that.data.goods_details.goods_info.kanjia_promotion == 1) {
      var is_kanjia = that.data.goods_details.goods_info.kanjia_promotion;
    }
    var ifcart = '';
    var log_id = '';
    var goods_id = e.currentTarget.dataset.goods_id;
    //item 店铺名，商品名，商品图片，选中规格，商品单价，数量
    var item = {};
    item.store_name = that.data.goods_details.store_info.store_name;
    item.goods_name = that.data.goods_details.goods_info.goods_name;
    item.goods_price = that.data.goods_price;
    item.spec_name = that.data.propertyChildNames;
    item.goods_num = that.data.num;
    item = JSON.stringify(item);
    var totalPrice = e.currentTarget.dataset.kanjia_price;
    var cart_id = goods_id + '|1';
    var item1 = JSON.parse(item);

    var item1 = JSON.parse(item);
    var storega = that.data.goods_storage;
    var arr = item1.spec_name.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?status=1&cart_id=' + cart_id + '&ifcart=' + ifcart + '&is_kanjia=' + is_kanjia,
      });
    } else {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      if (arr.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格尺寸哟~',
          showCancel: false,
        });
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?status=1&cart_id=' + cart_id + '&ifcart=' + ifcart + '&is_kanjia=' + is_kanjia,
      });
    }
  },
  //去拼团
  goTap: function (e) {
    var that = this;
    var goods_id = e.currentTarget.dataset.goods_id;
    var buyer_id = e.currentTarget.dataset.buyer_id;
    var log_id = e.currentTarget.dataset.log_id;
    var idx = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/join/index?buyer_id=' + buyer_id + '&log_id=' + log_id + '&goods_id=' + goods_id + '&index=' + idx,
    });
  },
  /**
   * 发起拼团
   */
  toCreateGroup: function (e){
    var that = this;
    var img = that.data.imgUrl[0];
    that.setData({
      showPin: true,
      goods_img: img
    });  
    var dataset = e.currentTarget.dataset;      
  },
  /**
   * 立即拼团
   */
  groupNow: function (e){
    var that = this;
    /**
     * log_id 参团id,自己发起的团,支付成功后返回的id，有则传，无则不传，不能穿空    pintuan_id 不为空时，为拼主的拼团号
     * pintuan    拼团的话设置为1，默认为空
     * status 1普通购买，2拼团，3秒杀
     */
    if (that.data.goods_details.goods_info.pintuan_promotion == 1) {
      var is_pintuan = that.data.goods_details.goods_info.pintuan_promotion;
    }
    var ifcart = '';
    var log_id = '';
    var goods_id = e.currentTarget.dataset.goods_id;
    var item = {};
    //item 店铺名，商品名，商品图片，选中规格，商品单价，数量
    var item = {};
    item.store_name = that.data.goods_details.store_info.store_name;
    item.store_id = that.data.goods_details.store_info.store_id;
    item.goods_name = that.data.goods_details.goods_info.goods_name;
    item.goods_price = that.data.goods_price;
    item.spec_name = that.data.propertyChildNames;
    item.goods_num = that.data.num;
    item = JSON.stringify(item);
    var cart_id = goods_id + '|1';
    var item1 = JSON.parse(item);
    var storega = that.data.goods_storage;
    var arr = item1.spec_name.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
        })
        this.setData({
          showView: false
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?&cart_id=' + cart_id + '&ifcart=' + ifcart + '&is_pintuan=' + is_pintuan,
      });
    } else {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      if (arr.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格尺寸哟~',
          showCancel: false,
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?&cart_id=' + cart_id + '&ifcart=' + ifcart + '&is_pintuan=' + is_pintuan,
      });
    }
  },
  /**
   * 单独购买
   */
  singPurchase: function (){
    var that = this;
    var img = that.data.imgUrl[0];
    that.setData({
      single: true,
      goods_img: img
    });
  },
  /**
   * 加入购物车--展开弹窗
   * @author chelfinn
   * @date 2018-09-19
   */
  addToCar: function (e) {
    var that = this;
    var img = that.data.imgUrl[0];
    var mobile = wx.getStorageSync("mobile");
    if (!mobile) {
      wx.navigateTo({
        url: '/pages/phone/index',
      })
    };
    that.setData({
      shopType: 'addShopCar',
      goods_img: img
    });
    that.bindGuiGeTap(e);
  },
  /**
   * 加入购物车/立即购买--展开弹窗
   */
  bindGuiGeTap: function (e) {
    var that = this;
    var img = that.data.imgUrl[0];
    that.setData({
      showView: true,
      goods_img: img
    });
    var dataset = e.currentTarget.dataset;
  },
  /**
   * 立即购买--展开弹窗
   * @author chelfinn
   * @date 2018-09-19
   */
  toBuy: function (e) {
    var that = this;
    var img = that.data.imgUrl[0];
    var mobile = wx.getStorageSync("mobile");
    if (!mobile) {
      wx.navigateTo({
        url: '/pages/phone/index',
      })
    };
    that.setData({
      shopType: 'tobuy',
      goods_img: img
    });
    that.bindGuiGeTap(e);
  },
  /**
   * 立即购买--跳转到订单页
   * @author chelfinn
   * @date 208-09-20
   */
  buyNow: function (e) {
    /**
     * log_id 参团id,自己发起的团,支付成功后返回的id，有则传，无则不传，不能穿空    pintuan_id 不为空时，为拼主的拼团号
     * pintuan    拼团的话设置为1，默认为空
     * status 1普通购买，2拼团，3秒杀
     */
    var that = this;
    var ifcart = '';
    var cart_id = e.currentTarget.dataset.goods_id + '|' + that.data.num;
    // item 店铺名，商品名，商品图片，选中规格，商品单价，数量
    var item = {};
    item.store_name = that.data.goods_details.store_info.store_name;
    item.goods_name = that.data.goods_details.goods_info.goods_name;
    item.goods_price = that.data.goods_price;
    item.spec_name = that.data.propertyChildNames;
    item.goods_num = that.data.num;
    item = JSON.stringify(item);
    var item1 = JSON.parse(item);
    var storega = that.data.goods_storage;
    var arr = item1.spec_name.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?&cart_id=' + cart_id + '&ifcart=' + ifcart,
      });
    } else {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      if (arr.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格尺寸哟~',
          showCancel: false,
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?&cart_id=' + cart_id + '&ifcart=' + ifcart,
      });
    }
  },
  /**
   * 立即加入购物车--提交购买
   * @author chlefinn
   * @date 2018-09-18
   */
  addInCar: function (e) {
    var that = this;
    var name = this.data.propertyChildNames.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (this.data.goods_storage == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他商品~',
          showCancel: false
        });
        return false;
      };
    } else {
      if (this.data.goods_storage == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他商品~',
          showCancel: false
        });
        return false;
      };
      if (name.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格和尺寸~',
          showCancel: false,
        });
        return false;
      }
    }
    var quantity = e.currentTarget.dataset.quantity;
    var sign = that.without(quantity);
    if (!sign) {
      return false;
    }
    wx.request({
      url: url + '/mobile/index.php?act=member_cart&op=cart_add',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: {
        key: wx.getStorageSync('key'),
        goods_id: e.currentTarget.dataset.goods_id,
        quantity: quantity
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '加入购物车成功',
            icon: 'success',
            duration: 2000
          });
          this.setData({
            showView: false,
            propertyChildNames: '',
          });
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
          this.setData({
            showView: false,
            propertyChildNames: '',
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  /**
   * 关闭弹窗
   */
  errorTap: function () {
    this.setData({
      showView: false,
      showPin: false,
      showKan: false,
      propertyChildNames: '',
      num: 1,
    });
  },
  /**
   * 切换规格选中状态
   * @author chelfinn
   * @date 2018-09-18
   */
  clickTap: function (e) {
    var that = this;
    var ids = [];   // 选中的id
    var valus = [];  // 选中商品的名字
    var specALLArr = that.data.specALLArr;
    var childs = that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData;
    // 取消该分类下的子栏目所有的选中状态
    for (var i = 0; i < childs.length; i++) {
      that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData[i].active = false;
    }
    // 设置当前选中状态
    that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData[e.currentTarget.dataset.propertychildindex].active = true;
    that.setData({
      specALLArr: specALLArr
    });
    // 循环数组 获取点中状态时的规格
    for (var id in specALLArr) {
      for (var i in specALLArr[id].twoData) {
        if (specALLArr[id].twoData[i].active == true) {
          ids.push(specALLArr[id].twoData[i].id);  // 获取的商品选中时的id
          valus.push(specALLArr[id].twoData[i].name);   // 获取商品点中时的规格
        }
      };
    };
    var spec_id = ids.sort().join("|");   // 升序
    var spec_value = valus.toString().replace(/,/g, " ");  // 选中的规格
    var spec_list = that.data.detailData.spec_list;
    if (spec_value) {
      this.setData({
        propertyChildNames: spec_value,
      })
    }
    if (spec_id in spec_list) {
      that.setData({
        buyGoods_id: spec_list[spec_id], //获取购买的商品id
      });
      // 获取商品详情
      wx.request({
        url: url + '/mobile/index.php?act=goods&op=goods_detail',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        dataType: 'json',
        method: 'GET',
        data: {
          goods_id: that.data.buyGoods_id,
        },
        success: (res) => {
          if (res.data.code == 200) {
            var datas = res.data.datas;
            var specObj = res.data.datas.goods_info.spec_name;
            var tmp = {}; //商品规格
            var tmp2 = {}; //商品尺寸
            var specALLArr = []; // 商品贵和和商品
            for (var i in specObj) {
              tmp.id = i;
              tmp.name = specObj[i];
              tmp.twoData = [];
              for (var j in res.data.datas.goods_info.spec_value[i]) {
                tmp2.id = j;
                tmp2.name = res.data.datas.goods_info.spec_value[i][j];
                tmp2.propertyID = i;
                tmp.twoData.push(JSON.parse(JSON.stringify(tmp2)));
              }
              specALLArr.push(JSON.parse(JSON.stringify(tmp)));
            };
            if (datas.goods_info.promotion_type == '') {
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
                goods_price: datas.goods_info.goods_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true,
              })
            } else if (datas.goods_info.pintuan_promotion == 1) {    // 拼团
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
                goods_price: datas.goods_info.pintuan_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true,
              })
            } else if (datas.goods_info.kanjia_promotion == 1) {    // 砍价
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
                goods_price: datas.goods_info.kanjia_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true,
              })
            } else {
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
                goods_price: datas.goods_info.goods_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true
              });
            };
            if (that.data.goods_storage == 0) {
              this.setData({
                showSure: false
              });
            } else {
              that.setData({
                showSure: true
              })
            }; 
          };
          if (res.data.code == 400) {
            if (res.data.login == 0 && res.data.login) {
              wx.navigateTo({
                url: '/pages/authorize/index',
              })
            } else {
              wx.showToast({
                title: res.data.datas.error,
                icon: 'none',
                duration: 1500,
                mask: true
              })
            };
          }
        },
        fail: () => {
          wx.showToast({
            title: '人太多啦',
            icon: 'none',
            duration: 2000
          })
        }
      });
    }
  },
  //点击减号
  minuAcvive: function () {
    var that = this;
    var num = that.data.num;
    if (num > 1) {    //如果num大于1才能减
      num--;
    };
    // 将数值与状态写回	
    that.setData({
      num: num,
    });
  },
  // 点击加号 
  addActive: function () {
    var that = this;
    var num = that.data.num;
    num++;
    // 将数值与状态写回
    that.setData({
      num: num,
    });
  },
  /**
   * 输入框事件
   */
  bindManual: function (e) {
    var that = this;
    var disabled;
    var num = e.detail.value;
    if (num == 0) {
      disabled = true;
    } else {
      disabled = false;
    }
    // 将数值与状态写回
    that.setData({
      num: num,
      disabled: disabled,
    });
  },
  
  /**
   * 改变banner上的数值
   */
  swiperChange(res) {
    this.setData({
      current: res.detail.current + 1,
    });
  },
  /**
   * 获取店铺内推荐商品
   */
  getStoreRecommend: function (storeid) {
    var that = this;
    wx.request({
      url: url + '/mobile/index.php?act=store&op=store_goods',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        store_id: storeid,
        page: 8,
        key: 3
      },
      success: (res) => {
        if (res.data.code == 200) {
          that.setData({
            storeItemList: res.data.datas.goods_list
          });
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  /**
	 * 禁止购买超过库存
	 * @author chelfinn 
	 * @date 2018-09-22
	 */
  without: function (n) {
    // 判断购买数量是否超过库存
    var goods_storage = this.data.goods_storage;
    if (n > goods_storage) {
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true;
  },
  
  /**
   * 跳转到店铺页面
   */
  toStore: (e) => {
    wx.navigateTo({
      url: '/pages/store/home/index?store_id=' + e.currentTarget.dataset.store_id,
    });
  },
  /**
   * 跳转商品详情页
   */
  toGoodsDetails: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/group-detail/index?goods_id=' + goods_id,
    });
  },
  // 跳转到首页
  toIndex: function() {
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var goods_detail =  this.data.goods_details;
    if (goods_detail.goods_hair_info.content == '免运费') {
      var title = '【包邮】￥' + this.data.goods_price + goods_detail.goods_info.goods_name;
    } else {
      var title = '￥' + this.data.goods_price + goods_detail.goods_info.goods_name;
    }
    return {
      title: title,
      imageUrl: goods_detail.goods_url,
    }
  }
})